﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using DBaseReader;

/*
    DBaseTestHarness
    Nicholas Bambury 
    Created September 2011.

    This is a simple test Harness to accompany the DBaseReader .Net DLL.

    Licence - Microsoft  Public Licence. See http://dbasereader.codeplex.com/license for details.
 
*/

namespace DBaseTestHarness
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private DBaseReader.DBaseReader dBase3 = null;
        private string path = null;
        private System.Collections.Generic.List<string> records;

        public MainWindow()
        {
            InitializeComponent();
            dBase3 = new DBaseReader.DBaseReader();
        }

        private void openDB_bt_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.Filter = "dbf files (*.dbf)|*.dbf";

            openFileDialog.ShowDialog();

            if (String.IsNullOrEmpty(openFileDialog.FileName))
                System.Windows.MessageBox.Show("No DBF File Selected", "DBF file Input");
            else
            {
                database_tb.Text = openFileDialog.FileName;
                int pos = database_tb.Text.LastIndexOf('\\');

                path = database_tb.Text.Substring(0, pos);
                                
                if (dBase3.OpenDBaseDBF(database_tb.Text))                
                {
                    System.Collections.Generic.List<string> temp2 = dBase3.GetUnusedFields();                    

                    numOfEmptyFields_Label.Content = "Number of Empty Fields: " + temp2.Count.ToString();
                    numOfFields_Label.Content = "Number of Fields: " + dBase3.NumberOfFields().ToString();
                    numOfRecords_Label.Content = "Number of Records: " + dBase3.NumberOfRecords().ToString();
                    numOfValidRecords_Label.Content = "Number of Valid Records: " + dBase3.NumberOfValidRecords().ToString();
                    isDataEncrypted_Label.Content = "Is Data Encrypted: " + dBase3.GetIsEncrypted().ToString();

                    System.Windows.MessageBox.Show("Database opened successfully", "Open Database");
                }
                else
                    System.Windows.MessageBox.Show("Error opening database", "Open Database");
            }
        }

        private void exit_bt_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void extract_bt_Click(object sender, RoutedEventArgs e)
        {
            if (path == null)
            {
                openDB_bt_Click(this,null);
            }
            try
            {
                dataGrid_fieldInfo.ItemsSource = dBase3.GetFieldsInfo();
                int col = dataGrid_fieldInfo.Columns.Count;

                dBase3.ExportUnusedFields();
                dBase3.ExportFieldData();
                dBase3.ExportData(DBaseReader.DBaseReader.ExportType.csvFile);

                dBase3.ExportUsedFieldsData();

                System.Collections.Generic.List<string> usedFieldsArray = dBase3.GetUsedFields();

                System.Collections.Generic.List<int> fieldNumList = new System.Collections.Generic.List<int>();

                string[] fieldNames = dBase3.GetFieldNames().Split(',');

                int i = 1;

                foreach (string field in fieldNames)
                {
                    if (i > 6)
                    {
                        System.Collections.IEnumerator iEnum = usedFieldsArray.GetEnumerator();

                        while (iEnum.MoveNext())
                        {
                            if (field == (string)iEnum.Current)
                                fieldNumList.Add(i);
                        }
                    }
                    i++;
                }

                records = dBase3.GetRecords(fieldNumList);

                System.IO.File.WriteAllLines(path + "\\UsedData.csv", (string[])records.ToArray());
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show("Export Data", ex.Message);
            }

            System.Windows.MessageBox.Show("Data Exported successfully", "Export Data");
        }

        private void button_GetData_Click(object sender, RoutedEventArgs e)
        {
            int field = 0;
            Int32.TryParse(textBox_ColumnNo.Text, out field);
            int record = 0;
            Int32.TryParse(textBox_RecordNo.Text, out record);

            Object data = dBase3.GetDataAsCorrectType(field, record);

            label_FieldType.Content = "Field Type: " + data.GetType().ToString();
            label_FieldData.Content = "Field Data: " + data.ToString();
        }

        private void button_FindData_Click(object sender, RoutedEventArgs e)
        {
            DataProcessing dataProcessing = new DataProcessing();

            System.Collections.ArrayList recordArray = new System.Collections.ArrayList();

            if (records == null)
            {
                extract_bt_Click(this, e);
            }

            recordArray.AddRange(records);
           
            System.Collections.Generic.List<DataProcessing.DataPosition> itemsFound = dataProcessing.findDataContaining(textBox_FindData.Text, recordArray);

            System.Collections.Generic.List<string> temparray = new System.Collections.Generic.List<string>();

            foreach (DataProcessing.DataPosition data in itemsFound)
            {
                String tempString = data.col.ToString() + "," + data.row.ToString();
                temparray.Add(tempString);
            }

            System.IO.File.WriteAllLines(path + "\\foundData.cvs", (string[])temparray.ToArray());

            label_DataFound.Content = label_DataFound.Content + itemsFound.Capacity.ToString();
        }

        private void button_FindPostCodes_Click(object sender, RoutedEventArgs e)
        {
            DataProcessing dataProcessing = new DataProcessing();

            System.Collections.ArrayList recordArray = new System.Collections.ArrayList();

            if (records == null)
            {
                extract_bt_Click(this, e);                
            }

            recordArray.AddRange(records);

            System.Collections.Generic.List<DataProcessing.DataPosition> itemsFound = dataProcessing.findPostCodes(recordArray);

            System.Collections.Generic.List<string> temparray = new System.Collections.Generic.List<string>();

            foreach (DataProcessing.DataPosition data in itemsFound)
            {
                String tempString = data.col.ToString() + "," + data.row.ToString();
                temparray.Add(tempString);
            }

            System.IO.File.WriteAllLines(path + "\\foundPostCode.cvs", (string[])temparray.ToArray());

            label_PostCodesFound.Content = label_PostCodesFound.Content + itemsFound.Capacity.ToString();
        }

        private void button_ExpRegData_Click(object sender, RoutedEventArgs e)
        {
            DataProcessing dataProcessing = new DataProcessing();

            System.Collections.ArrayList recordArray = new System.Collections.ArrayList();

            if (records == null)
            {
                extract_bt_Click(this, e);
            }

            recordArray.AddRange(records);

            System.Collections.Generic.List<DataProcessing.DataPosition> itemsFound = dataProcessing.findDataMatchingRegex(textBox_RegExp.Text, recordArray);

            System.Collections.Generic.List<string> temparray = new System.Collections.Generic.List<string>();

            foreach (DataProcessing.DataPosition data in itemsFound)
            {
                String tempString = data.col.ToString() + "," + data.row.ToString();
                temparray.Add(tempString);
            }

            System.IO.File.WriteAllLines(path + "\\foundRegExpData.cvs", (string[])temparray.ToArray());

            label_RegExpFound.Content = label_RegExpFound.Content + itemsFound.Capacity.ToString();
        }
    }
}
